using System;
using System.Diagnostics;
using Dida.Dbg;

namespace Dida
{
	public abstract class Actor : ISpatialHashObject
	{
		World world;
		SimpleTransform prevTransform;
		SimpleTransform transform;

		object hashObjState = null;

		public World World
		{
			get
			{
				Debug.Assert(world != null, "This actor (a " + GetType().FullName +
					") hasn't been added to a world yet.");
				return world;
			}

			internal set
			{
				Debug.Assert(world == null, "You can't add an actor to a world, " +
					"that's already added to another (or the same) world");

				world = value;
				OnAddedToWorld();
			}
		}

		protected virtual void OnAddedToWorld()
		{
			if (AddedToWorld != null)
				AddedToWorld(this, new EventArgs());
		}
		public event EventHandler AddedToWorld;

		protected abstract SimpleTransform GetTransform();

		public SimpleTransform PrevTransform
		{
			get { return prevTransform; }
		}

		public SimpleTransform Transform
		{
			get { return transform; }
		}

		public virtual void Update(float timeDelta)
		{
			prevTransform = transform;
			transform = GetTransform();
		}

		public virtual void Start() { }
		public virtual void Stop() { }

		public virtual void DrawDebug(IDebugDrawTarget target)
		{
			target.DrawBox(BoundingBox, 0x00ff0000);
		}

		#region ISpatialHashObject related

		/// <summary>
		/// Should get the world space bounding box of this object.
		/// If this object is animated, the animation should be applied to this
		/// bounding box by the implementation.
		/// </summary>
		public abstract Box BoundingBox
		{
			get;
		}

		object ISpatialHashObject.HashObjState
		{
			get { return hashObjState; }
			set { hashObjState = value; }
		}

		#endregion
	}
}